package com.phiture.erp.produce.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.produce.core.pojo.vo.BomGroupStandardListReqVO;
import com.phiture.erp.produce.core.pojo.vo.BomGroupStandardRespVO;
import com.phiture.erp.produce.core.pojo.vo.BomGroupStandardSaveReqVO;
import com.phiture.erp.produce.core.service.BomGroupStandardService;
import com.phiture.erp.produce.dal.entity.BomGroupStandardDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - bom分组")
@RestController
@RequestMapping("/erp/bom-group-standard")
@Validated
public class BomGroupStandardController {

    @Resource
    private BomGroupStandardService bomGroupStandardService;

    @PostMapping("/create")
    @Operation(summary = "创建bom分组")
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:create')")
    public CommonResult<Long> createBomGroupStandard(@Valid @RequestBody BomGroupStandardSaveReqVO createReqVO) {
        return success(bomGroupStandardService.createBomGroupStandard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新bom分组")
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:update')")
    public CommonResult<Boolean> updateBomGroupStandard(@Valid @RequestBody BomGroupStandardSaveReqVO updateReqVO) {
        bomGroupStandardService.updateBomGroupStandard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除bom分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:delete')")
    public CommonResult<Boolean> deleteBomGroupStandard(@RequestParam("id") Long id) {
        bomGroupStandardService.deleteBomGroupStandard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得bom分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:query')")
    public CommonResult<BomGroupStandardRespVO> getBomGroupStandard(@RequestParam("id") Long id) {
        BomGroupStandardDO bomGroupStandard = bomGroupStandardService.getBomGroupStandard(id);
        return success(BeanUtils.toBean(bomGroupStandard, BomGroupStandardRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得bom分组列表")
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:query')")
    public CommonResult<List<BomGroupStandardRespVO>> getBomGroupStandardList(@Valid BomGroupStandardListReqVO listReqVO) {
        List<BomGroupStandardDO> list = bomGroupStandardService.getBomGroupStandardList(listReqVO);
        return success(BeanUtils.toBean(list, BomGroupStandardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出bom分组 Excel")
    @PreAuthorize("@ss.hasPermission('erp:bom-group-standard:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBomGroupStandardExcel(@Valid BomGroupStandardListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<BomGroupStandardDO> list = bomGroupStandardService.getBomGroupStandardList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "bom分组.xls", "数据", BomGroupStandardRespVO.class,
                        BeanUtils.toBean(list, BomGroupStandardRespVO.class));
    }

}